﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using TestService.ApplicationService.Services.Interface;
using TestService.Contract.Data;

namespace TestService.ApplicationService.Services.Impl
{
    public class XmlParserService : IXmlParserService
    {
        private MessageModel msg;

        public XmlParserService(MessageModel msg)
        {
            this.msg = msg;
        }

        public RealTimeEventMessage ParseRawMsmqXml(string messageBody)
        {
            try
            {
                RealTimeEventMessage info = new RealTimeEventMessage();
                XElement xelement = XElement.Parse(messageBody);
                MessageModel model = new MessageModel();
                model.MessageId = GetSafeString(xelement, "messageId");
                model.Content = GetSafeString(xelement, "content");
                model.CreateTime = GetSafeDate(xelement, "createTime") ?? DateTime.Now;

                info.Msg = model;
                info.EventName = GetSafeString(xelement, "eventName");
                //info.EntityIdType = GetSafeString(xelement, "entityIdType");
                //info.Description = GetSafeString(xelement, "description").Replace("\n\n", "\n\r");
                //info.Date = GetSafeDate(xelement, "date");
                //info.AdditionalData = GetSafeString(xelement, "additionalData");
                return info;

            }
            catch (Exception ex)
            {
                Log.Error("解析Xml消息出现异常：" + ex);
                return null;
            }
        }

        public static Int32 GetSafeInt32(XElement root, string elementName)
        {
            try
            {
                XElement element = root.Elements().Where(node => node.Name.LocalName == elementName).Single();
                return Convert.ToInt32(element.Value);
            }
            catch
            {
                return 0;
            }
        }

        private static DateTime? GetSafeDate(XElement root, string elementName)
        {
            try
            {
                XElement element = root.Elements().Where(node => node.Name.LocalName == elementName).Single();
                return DateTime.Parse(element.Value);
            }
            catch
            {
                return null;
            }
        }

        public static String GetSafeString(XElement root, string elementName)
        {
            try
            {
                XElement element = root.Elements().Where(node => node.Name.LocalName == elementName).Single();
                return element.Value;
            }
            catch
            {
                return String.Empty;
            }
        }

        public static bool GetSafeBool(XElement root, string elementName)
        {
            try
            {
                XElement element = root.Elements().Where(node => node.Name.LocalName == elementName).Single();
                return Convert.ToBoolean(element.Value);
            }
            catch
            {
                return false;
            }
        }
    }
}
